package vn.edu.saigontech.ncms.web.offering.report;

import java.util.*;

import vn.edu.saigontech.ncms.model.info.Offering;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.service.PlacementTestService;
import vn.edu.saigontech.ncms.service.StudentTypeService;
import vn.edu.saigontech.ncms.util.DateUtil;

public class MonthlyOfferingReport {

	private int month;
	private int year;
	
	private Collection<Offering> offerings;
	private Collection<Offering> classifiedOfferings;
	private Collection<Offering> newOfferings = new ArrayList<Offering>();
	private Collection<Offering> endOfferings = new ArrayList<Offering>();
	
	private int numberOfOfferings = 0;
	private int numberOfActualOfferings = 0;
	
	private int numberOfStudents = 0;
	private int numberOfActualStudents = 0;
	
	private int numberOfSGTMembers = 0;
	private int numberOfActualSGTMembers = 0;
	
	private int numberOfNewOfferings = 0;
	private int numberOfEndOfferings = 0;
	
	
	public MonthlyOfferingReport(int month, int year) {
		this.month = month;
		this.year = year;
		this.init();
	}
	
	private void init() {
		this.offerings = OfferingService.INSTANCE.findByMonthAndYear(this.month, this.year);
		List<Offering> remainings = new ArrayList<Offering>();
		
		for(Offering offering : this.offerings) {
			this.numberOfOfferings++;
			this.numberOfStudents += offering.getNumberOfStudents();
			this.numberOfActualStudents += offering.getNumberOfStudents();
			this.numberOfSGTMembers += offering.getNumberOfStudents(StudentTypeService.INSTANCE.findById(0L));
			if(DateUtil.inSameMonth(DateUtil.newDate(1, this.month, this.year), offering.getStartDate())) {
				this.numberOfNewOfferings++;
				this.newOfferings.add(offering);
			}
			else if(DateUtil.inSameMonth(DateUtil.newDate(1, this.month, this.year), offering.getEndDate())) {
				this.numberOfEndOfferings++;
				this.numberOfActualStudents -= offering.getNumberOfStudents();
				this.numberOfSGTMembers -= offering.getNumberOfStudents(StudentTypeService.INSTANCE.findById(0L));
				this.endOfferings.add(offering);
			}
			else {
				remainings.add(offering);
			}
		}
		this.numberOfActualOfferings = this.numberOfOfferings - this.numberOfEndOfferings;
		this.classifiedOfferings = new ArrayList<Offering>();
		this.classifiedOfferings.addAll(this.endOfferings);
		this.classifiedOfferings.addAll(this.newOfferings);
		this.classifiedOfferings.addAll(remainings);
	}
	
	public int getNumberOfSGTMembers(Offering offering) {
		return offering.getNumberOfStudents(StudentTypeService.INSTANCE.findById(0L));
	}
	
	public int getNumberOfPlacementTests() {
		return PlacementTestService.INSTANCE.countInMonth(this.month, this.year);
	}
	
	public int getNumberOfMakeUpTests() {
		return 0;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public Collection<Offering> getOfferings() {
		return offerings;
	}

	public void setOfferings(Collection<Offering> offerings) {
		this.offerings = offerings;
	}

	public Collection<Offering> getClassifiedOfferings() {
		return classifiedOfferings;
	}

	public void setClassifiedOfferings(Collection<Offering> classifiedOfferings) {
		this.classifiedOfferings = classifiedOfferings;
	}

	public Collection<Offering> getNewOfferings() {
		return newOfferings;
	}

	public void setNewOfferings(Collection<Offering> newOfferings) {
		this.newOfferings = newOfferings;
	}

	public Collection<Offering> getEndOfferings() {
		return endOfferings;
	}

	public void setEndOfferings(Collection<Offering> endOfferings) {
		this.endOfferings = endOfferings;
	}

	public int getNumberOfOfferings() {
		return numberOfOfferings;
	}

	public void setNumberOfOfferings(int numberOfOfferings) {
		this.numberOfOfferings = numberOfOfferings;
	}

	public int getNumberOfActualOfferings() {
		return numberOfActualOfferings;
	}

	public void setNumberOfActualOfferings(int numberOfActualOfferings) {
		this.numberOfActualOfferings = numberOfActualOfferings;
	}

	public int getNumberOfStudents() {
		return numberOfStudents;
	}

	public void setNumberOfStudents(int numberOfStudents) {
		this.numberOfStudents = numberOfStudents;
	}

	public int getNumberOfActualStudents() {
		return numberOfActualStudents;
	}

	public void setNumberOfActualStudents(int numberOfActualStudents) {
		this.numberOfActualStudents = numberOfActualStudents;
	}

	public int getNumberOfSGTMembers() {
		return numberOfSGTMembers;
	}

	public void setNumberOfSGTMembers(int numberOfSGTMembers) {
		this.numberOfSGTMembers = numberOfSGTMembers;
	}

	public int getNumberOfActualSGTMembers() {
		return numberOfActualSGTMembers;
	}

	public void setNumberOfActualSGTMembers(int numberOfActualSGTMembers) {
		this.numberOfActualSGTMembers = numberOfActualSGTMembers;
	}

	public int getNumberOfNewOfferings() {
		return numberOfNewOfferings;
	}

	public void setNumberOfNewOfferings(int numberOfNewOfferings) {
		this.numberOfNewOfferings = numberOfNewOfferings;
	}

	public int getNumberOfEndOfferings() {
		return numberOfEndOfferings;
	}

	public void setNumberOfEndOfferings(int numberOfEndOfferings) {
		this.numberOfEndOfferings = numberOfEndOfferings;
	}
}
